<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 支付回调通知
 */
namespace app\allwin\controller\api\v4;
use app\allwin\controller\api\Base;
use app\common\facade\WechatPay;
use app\allwin\model\Order;
use app\allwin\model\Bank;
use app\allwin\model\BankBill;
use app\allwin\model\AllwinGiftData;
use app\allwin\model\CouponUser;
use app\allwin\model\Vip;
use app\allwin\widget\Order as OrderIncome;
use filter\Filter;
use Exception;

class Paymentnotify extends Base{

     /**
     *  微信买单消费通知
     * @return void
     */
    public function index(){
        try {
            $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function ($message,$fail) {
                $result = Order::where(['state'=>0,'order_no' => Filter::filter_escape($message['out_trade_no'])])->find();
                if (empty($result)) {
                    return true;
                }
                if ($message['return_code'] === 'SUCCESS') {
                    if ($message['result_code'] === 'SUCCESS') {
                        $sub_mchid = $message['sub_mch_id'] ?? '';
                        $ispay = WechatPay::doPay($this->miniapp_id,false,$sub_mchid)->order->queryByOutTradeNumber($result->order_no);
                        if ($ispay['return_code'] === 'SUCCESS') {
                            if ($ispay['result_code'] === 'SUCCESS') {
                                if ($ispay['trade_state'] === 'SUCCESS'){
                                    //订单状态
                                    $result->state     = 1;
                                    $result->paid_time = strtotime($ispay['time_end']);
                                    $result->paid_no   = $ispay['transaction_id'];
                                    $result->save();
                                    //收益计算
                                    OrderIncome::income($result->toArray());
                                    //消费日志
                                    BankBill::add(['miniapp_id' => $this->miniapp_id,'store_id' => $result->store_id,'money' => -$result->order_amount,'uid' => $result->uid,'pay_uid' => $result->uid],'微信支付[消费]');
                                    //支付有礼
                                    AllwinGiftData::giveGift($result,$this->miniapp_id);
                                    //积分奖励
                                    Bank::rePoints($this->miniapp_id,$result->uid,intval($result->order_amount));
                                    //领取优惠券
                                    CouponUser::addUserCoupon($result->coupon_ids,$result->uid); //领取的优惠券
                                    //使用优惠券
                                    if ($result->coupon_user_id > 0) {
                                        CouponUser::userCouponPay($result);    //优惠券减少
                                        OrderIncome::fund($result->toArray()); //采购基金补贴
                                    }
                                    //买单即开通会员
                                    Vip::regVip($result);
                                    return true;
                                }
                            }
                        }
                    }
                }
                return $fail('通信失败,请稍后再通知我');
            });
            $response->send();
        }catch (Exception $e) {
            $this->error('页面不存在');
        }
    }
}